package com.sc.nft.factory;

import com.sc.nft.service.ApplicationRequirementFactoryService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ApplicationRequirementFactory implements ApplicationContextAware {
    private static Map<Integer, ApplicationRequirementFactoryService> applicationRequirementFactoryServiceMap;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, ApplicationRequirementFactoryService> map = applicationContext.getBeansOfType(ApplicationRequirementFactoryService.class);
        applicationRequirementFactoryServiceMap = new HashMap<>();
        map.forEach((key, value) -> applicationRequirementFactoryServiceMap.put(value.code(), value));
    }
    public  <T extends ApplicationRequirementFactoryService> T getApplicationRequirementFactoryService(Integer code) {
        return (T) applicationRequirementFactoryServiceMap.get(code);
    }
}
